package org.jsbd.boss.task;

import org.jsbd.boss.common.redis.RedisClient;
import org.jsbd.boss.service.impl.TaskUtil;
import org.jsbd.boss.util.CacheKey;
import org.jsbd.boss.util.OssUtils;
import org.jsbd.boss.util.SpringContextUtils;
import org.jsbd.boss.util.ZipUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.Serializable;

/**
 * @desc
 * @Date 2018/9/15 16:00
 * @Author xupengcheng [xupengcheng@zhexinit.com]
 */
public class OssTask implements Runnable, Serializable {

    public static final Logger logger = LoggerFactory.getLogger(OssTask.class);

    private static final long serialVersionUID = 1L;
    private String task;

    private String imsi;


    private int apkId;

    private String mark;

    private int taskType;

    private String taskDay;

    public static final String BUCKET_NAME = "webmeng";

    public OssTask(String task, String imsi, int taskType, int apkId, String taskDay, String mark) {
        this.task = task;
        this.taskType = taskType;
        this.imsi = imsi;
        this.taskDay = taskDay;
        this.apkId = apkId;
        this.mark = mark;
    }

    @Override
    public void run() {
        try {
            long start = System.currentTimeMillis();
            String fileName = TaskUtil.getTaskFileName(imsi, taskType, apkId, taskDay);
            byte[] bytes = ZipUtil.zipFile(task, imsi);
            OssUtils.uploadFile(BUCKET_NAME, fileName, bytes);
            logger.warn("上传任务文件 {} {}", fileName, System.currentTimeMillis() - start);
            RedisClient redisClientTemp = SpringContextUtils.getBean("redisClientTemp");
            redisClientTemp.put(CacheKey.A_RETENT_APK_RECORD + imsi + ":" + apkId, "1", 180 * 24 * 3600);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("上传任务文件失败 {} ", e);
        }
    }


}
